<template>
	<view>
		<view class='search acea-row row-between-wrapper'>
			<view class='input acea-row row-between-wrapper'>
				<text class='iconfont icon-sousuo2'></text>
				<input type='text' :value='searchValue' :focus="focus" placeholder='请输入患者名称'
					placeholder-class='placeholder' @input="setValue"></input>
			</view>
			<view class='bnt' @tap='searchBut'>搜索</view>
			<view class="bnt small" @click.stop="clickFilter">
				<text class="yyfont yy-shaixuan"></text>筛选
			</view>
			<view class="bnt small" @click.stop="clickSort">
				<text v-if="sortType == 0" class="yyfont yy-sort-ascending"></text>
				<text v-else class="yyfont yy-sort-descending"></text>排序
			</view>
		</view>
		<view class="nav">
			<uni-segmented-control ref="segRef" :current="current" :values="items" :style-type="styleType"
				:active-color="activeColor" @clickItem="onClickItem" />
		</view>
		<view class="content">
			<view v-if="current === 0">
				<view class="asking-box" v-if="askingList.length>0">
					<view class="list-item" v-for="(item,index) in askingList" @click="clickTalk(item)">
						
						<text class="asknum" v-if="item.num && item.num>0">{{item.num}}</text>
						<view class="item-top">
							<!-- 咨询时间: {{item.create_time}} -->
							<view v-if="item.type == 2" class="item-time">
								预约时间: {{item.compile_time}}
							</view>
							<view v-else class="item-time">
								咨询时间: {{item.create_time}}
							</view>
							<view class="item-type-box">
								<view class="item-type video" v-if="item.type == 2">
									<text class="yyfont yy-record-video">视频问诊</text>
								</view>
								<view v-else class="item-type normal">
									<text class="yyfont yy-tubiaopinglunshu">普通问诊</text>
								</view>
							</view>
						</view>
						<view class="bot-dot-box">
							<view class="dot left"></view>
							<view class="line"></view>
							<view class="dot right"></view>
						</view>
						<view class="item-bot">
							<view class="doctor">
								咨询医生: {{item.doctor_name}}
							</view>
							<view class="patient-box">
								<view class="patient">
									咨询患者: {{getShowName(item)}}
								</view>
								<!-- <view :class="item.conversation_status == 1 ?'':'status' ">{{getStatus(item)}}</view> -->
								<view v-if="item.conversation_status == 1" class="status-finish">已完成</view>
								<view v-else-if="item.conversation_status == 2" class="status-cancel">已取消</view>
								<view v-else class="status-ing">未完成</view>
							</view>
						</view>
					</view>
					<view class='loadingicon acea-row row-center-wrapper' @click="clickMore(false)">
						<text class='loading iconfont icon-jiazai'
							:hidden='askingLoading==false'></text>{{askingLoadTitle}}
					</view>
				</view>
				<view class="nodata" v-else>
					<emptyPage title="暂无数据～"></emptyPage>
				</view>
			</view>
			<view v-if="current === 1">
				<view class="finish-box" v-if="finishList.length>0">
					<view class="list-item" v-for="(item,index) in finishList" @click="clickTalk(item)">
						
						<text class="asknum" v-if="item.num &&  item.num>0">{{item.num}}</text>
						<view class="item-top">
							<!-- 咨询时间: {{item.create_time}} -->
							<view v-if="item.type == 2" class="item-time">
								预约时间: {{item.compile_time}}
							</view>
							<view v-else class="item-time">
								咨询时间: {{item.create_time}}
							</view>
							<view class="item-type-box">
								<view class="item-type video" v-if="item.type == 2">
									<text class="yyfont yy-record-video">视频问诊</text>
								</view>
								<view v-else class="item-type normal">
									<text class="yyfont yy-tubiaopinglunshu">普通问诊</text>
								</view>
							</view>
						</view>
						<view class="bot-dot-box">
							<view class="dot left"></view>
							<view class="line"></view>
							<view class="dot right"></view>
						</view>
						<view class="item-bot">
							<view class="doctor">
								咨询医生: {{item.doctor_name}}
							</view>
							<view class="patient-box">
								<view class="patient">
									咨询患者: {{getShowName(item)}}
								</view>
								<!-- <view :class="item.conversation_status == 1 ?'':'status' ">{{getStatus(item)}}</view> -->
								<view v-if="item.conversation_status == 1" class="status-finish">已完成</view>
								<view v-else-if="item.conversation_status == 2" class="status-cancel">已取消</view>
								<view v-else class="status-ing">未完成</view>
							</view>
						</view>
					</view>
					<view class='loadingicon acea-row row-center-wrapper' @click="clickMore(false)">
						<text class='loading iconfont icon-jiazai'
							:hidden='finishLoading==false'></text>{{finishLoadTitle}}
					</view>
				</view>
				<view class="nodata" v-else>
					<emptyPage title="暂无数据～"></emptyPage>
				</view>
			</view>
		</view>
		<list-filter :alertShow="showSheet" :timeScope="timeScope" :askType="askType" @cancleAlert="cancleSheet" @changeTime="changeTime" @changeType="changeType"></list-filter>
		<view></view>
	</view>
</template>

<script>
	import emptyPage from '@/components/emptyPage.vue';
	
	import {
		docInquiryList
	} from '@/api/doc_appointment.js';
	import {
		getConversationStatus
	} from '@/api/inquiry.js';
	
	import {
		mapGetters
	} from "vuex";
	import listFilter from './listFilter.vue'
	export default {
		components: {
			emptyPage,
			listFilter
		},
		computed: {
			...mapGetters(['userInfo', 'uid','routeError'])
		},
		data() {
			return {
				showSheet:false,
				timeScope:0,
				askType:3,
				sortType:0,// 0-降序  1-升序
				searchValue: '',
				focus: false,

				items: ['待就诊', '已结束'],
				current: 0,
				activeColor: '#38ce8c',
				styleType: 'text',

				askingLoadTitle: '加载更多',
				askingLoading: false,
				askingLoadend: false,
				askingWhere: {
					'page': 1,
					'limit': 20,
					// 'type':'1',//0.图文问诊  1.在线问诊
					'name':''
				},
				askingList: [],
				finishLoadTitle: '加载更多',
				finishLoading: false,
				finishLoadend: false,
				finishWhere: {
					'page': 1,
					'limit': 20,
					// 'type':'1',//0.图文问诊  1.在线问诊【新加视频问诊,type=1时服务端处理返回type=1和2的总数据】
					'name':''
				},
				finishList: [],
				isSearch: false,
			}
		},
		onLoad() {
			if(this.routeError) {
				this.$store.commit('GO_ERROR');
				return
			}
			// this.getAskingList();
		},
		onShow() {
			this.$nextTick(()=>{
				this.searchBut();
			})
		},
		onPullDownRefresh() {
			console.log('rk==>refresh')
			this.clickMore(true);
		},
		onReachBottom() {
			this.clickMore(false);
		},

		methods: {
			/**
			 * 筛选
			 */
			clickFilter(){
				this.showSheet = true;
			},
			cancleSheet(){
				this.showSheet = false;
			},
			changeTime(val){
				this.timeScope = val;
				
				this.searchBut();
			},
			changeType(val){
				this.askType = val;
				
				this.searchBut();
			},
			/**
			 * 排序
			 */
			clickSort(){
				if(this.sortType == 0){
					this.sortType = 1;
				}else{
					this.sortType = 0;
				}
				console.log("rk===>[paixu]" + this.sortType);
				
				this.searchBut();
			},
			getShowName(item) {
				let patient_info = item.patient_info;
				if(patient_info){
					return patient_info.name;
				}
				return item.nickname;
			},
			// getStatus(item) {
			// 	let status = item.conversation_status == 1 ? '已完成' : '未完成';
			// 	return status;
			// },
			setValue: function(event) {
				this.$set(this, 'searchValue', event.detail.value);

				this.askingWhere.name = this.searchValue;
				this.finishWhere.name = this.searchValue;

				console.log('rk==>[...]' + JSON.stringify(this.searchValue));
				// 清空自动搜索
				if (this.searchValue == '') {
					this.searchBut();
				}
			},
			searchBut: function() {
				this.isSearch = true;
				this.onClickItem(this.$refs.segRef);
			},

			onClickItem(e) {

				console.log("rk===>[e]" + e.currentIndex);

				if (this.current !== e.currentIndex || this.isSearch) {
					this.current = e.currentIndex
					uni.stopPullDownRefresh();

					if (this.current == 1 ) {
						this.isSearch = false;
						this.finishLoadend = false;
						this.finishWhere.page = 1;
						this.getFinishList();
					}

					if (this.current == 0 ) {
						this.isSearch = false;
						this.askingLoadend = false;
						this.askingWhere.page = 1;
						this.getAskingList();
					}

				}
			},
			/**
			 * 更多
			 */
			clickMore(isRefresh) {

				if (this.current == 1) {

					if (this.finishLoadend && !isRefresh) {
						uni.stopPullDownRefresh();
						return
					}
					this.finishLoadend = false;
					if (isRefresh) {
						this.finishWhere.page = 1;
					}
					this.getFinishList();

				} else {
					if (this.askingLoadend && !isRefresh) {
						uni.stopPullDownRefresh();
						return
					}
					this.askingLoadend = false;
					if (isRefresh) {
						this.askingWhere.page = 1;
					}
					this.getAskingList();
				}
			},
			/**
			 * 进行中
			 */
			getAskingList() {
				let that = this;

				that.askingWhere.uid = this.uid;
				//进行中
				that.askingWhere.conversation_status = 0;

				that.askingWhere.type = this.askType;
				that.askingWhere.time = this.timeScope;
				that.askingWhere.sort = this.sortType;
				
				docInquiryList(that.askingWhere).then(res => {
					uni.stopPullDownRefresh();

					if (that.askingWhere.page == 1) {
						that.askingList = [];
					}
					let resList = res.data.list;
					let allCount = res.data.count;
					that.askingList = that.$util.SplitArray(resList, that.askingList);
					that.$set(that, 'askingList', that.askingList);
					let loadend = that.askingList.length >= allCount;

					console.log('rk==>[xx]' + allCount + '[xx]' + that.askingList.length + '[bool]' + loadend);

					that.askingLoadend = loadend;
					that.askingLoadTitle = loadend ? '我也是有底线的' : '加载更多';
					that.askingWhere.page += 1;
					that.askingLoading = false;
					
				}).catch(e=>{
					this.$util.Tips({
						title:e,
					});
				});

			},
			/**
			 * 已完成
			 */
			getFinishList() {
				let that = this;
				that.finishWhere.uid = this.uid;
				// 已完成
				that.finishWhere.conversation_status = 1;
				
				that.finishWhere.type = this.askType;
				that.finishWhere.time = this.timeScope;
				that.finishWhere.sort = this.sortType;
				
				docInquiryList(that.finishWhere).then(res => {
					uni.stopPullDownRefresh();

					if (that.finishWhere.page == 1) {
						that.finishList = [];
					}
					let resList = res.data.list;
					let allCount = res.data.count;
					that.finishList = that.$util.SplitArray(resList, that.finishList);
					that.$set(that, 'finishList', that.finishList);
					let loadend = that.finishList.length >= allCount;

					console.log('rk==>[xx]' + allCount + '[xx]' + that.finishList.length + '[bool]' + loadend);

					that.finishLoadend = loadend;
					that.finishLoadTitle = loadend ? '我也是有底线的' : '加载更多';
					that.finishWhere.page += 1;
					that.finishLoading = false;
					
				}).catch(e=>{
					this.$util.Tips({
						title:e,
					});
				});

			},
			/**
			 * 点击事件
			 */
			clickTalk(item) {
				console.log('rk==>[click]' + JSON.stringify(item.conversation_id));
			
				let where = {
					'conversation_id': item.conversation_id,
				}
				let that = this;
				getConversationStatus(where).then(res => {
					if (res.status == 200) {
						let resData = res.data;
						that.jumpUrl(item,resData);
					} else {
						that.$util.Tips({
							title: res.message
						})
					}
				}).catch(err => {
					console.log('rk===>' + JSON.stringify(err))
					that.$util.Tips({
						title: err
					});
				});
			},
			jumpUrl(item, resData) {
				
				// status:0-问诊中  1-已完成
				let status = resData.info;
				// type:0-图文  1-在线  2-视频
				let conversition_type = resData.type
				// can_refund: 1-能取消  0-不能取消【当type=2时有效】
				let can_refund = resData.can_refund;
				
				let yyMerId = item.mer_id;
				uni.navigateTo({
					url: '/pages/chat/customer_list/chath5?conversation_id='
					+ item.conversation_id 
					+'&conversation_status=' + status 
					+'&mer_id='+yyMerId
					+'&role=doctor'
					+'&conversition_type=' + conversition_type
					+'&can_refund=' + can_refund
				});
			},
			

		}
	}
</script>

<style lang="scss">
	page {}

	
	.search {
		padding: 20rpx 30rpx 20rpx 0;
		background-color: #fff;
	}

	.search .input {
		width: 358rpx;
		background-color: #f7f7f7;
		border-radius: 33rpx;
		padding: 0 35rpx;
		box-sizing: border-box;
		height: 66rpx;
		margin-left: 30rpx;
	}

	.search .input input {
		width: 252rpx;
		font-size: 28rpx;
	}

	.search .input .placeholder {
		color: #bbb;
	}

	.search .input .iconfont {
		color: #000;
		font-size: 35rpx;
	}

	.search .bnt {
		width: 90rpx;
		text-align: center;
		height: 66rpx;
		line-height: 66rpx;
		font-size: 30rpx;
		color: #282828;
	}
	.search .small {
		font-size: 26rpx;
	}
	.nav {
		background-color: #fff;
	}

	.content {
		margin: 20rpx 0rpx;

		.list-item {
			margin: 50rpx 30rpx;
			border-radius: 10px;
			background-color: #fff;
			font-size: 30rpx;
			padding: 20rpx 0rpx;
			
			position: relative;
			.asknum{
				min-width: 15px;
				height: 15px;
				text-align: center;
				background-color: #fff;
				color: #fff;
				background-color: $yy-main-col;
				border-radius: 15rpx;
				position: absolute;
				right: -10rpx;
				top: -10rpx;
				font-size: 10px;
				padding: 0 4px;
			}
		}
		.item-top {
			padding: 20rpx 30rpx 15rpx;
			//border-bottom: 1rpx dashed $yy-main-col;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 28rpx;
			.item-time {
				color: $yy-main-col;
				max-width: 70%;
			}
			.item-type-box {
				// min-width: 25%;
				.yyfont {
					font-size: 24rpx;
				}
				.yyfont:before {
					margin-right: 10rpx;
				}
				.item-type {
					padding: 6rpx 15rpx;
					border-radius: 10rpx;
				}
				.video {
					background-color: #DBF9EE;
				}
				.normal {
					background-color: #EBF1FE;
				}
			}
		}
		
		.bot-dot-box {
			height: 15rpx;
			display: flex;
		
			.dot {
				position: fixed;
				width: 30rpx;
				height: 30rpx;
				border-radius: 40rpx;
				background-color: #f5f5f5;
			}
		
			.left {
				left: 15rpx;
			}
		
			.right {
				right: 15rpx;
			}
		
			.line {
				width: 700rpx;
				border-bottom: 1rpx dashed $yy-main-col;
			}
		}
		
		.item-bot {
			padding: 30rpx 30rpx 0rpx;
		
			.doctor {}
		
			.patient-box {
				padding: 20rpx 0rpx;
				display: flex;
				justify-content: space-between;
			}
		
			.status-ing {
				color: #ff0000;
			}
			.status-cancel {
				color: #969696;
			}
		}

		
	}
</style>
